﻿using System;
using System.Collections.Generic;
using System.Collections;
using Jxym.DBUtility;
using Jxym.wlkc2011.Module.Default.model;
using System.Data.SqlClient;

namespace Jxym.wlkc2011.Module.CMS.SERVER
{
    public partial class MenuTree : System.Web.UI.Page
    {
        string strTree = "";
        //新模块ID列表
        ArrayList menus = null;
        MsSqlDal<User_TreeMenu> iUtm = new MsSqlDal<User_TreeMenu>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                strTree = "";
                //新模块ID列表
                menus = new ArrayList();
                menus = UserMenuList();
                TreeData(0);
                Response.Write(strTree);
            }
        }

        /// <summary>
        /// 获取用户菜单ID列表
        /// </summary>
        /// <returns></returns>
        public ArrayList UserMenuList()
        {
            //新模块ID列表
            ArrayList menuList = new ArrayList();
            MsSqlDal<User_RoleConfig> iUrc = new MsSqlDal<User_RoleConfig>();
            MsSqlDal<User_RolePermissionConfig> iUrpc = new MsSqlDal<User_RolePermissionConfig>();
            //用户ID
            string userID = Jxym.Common.Request.AdminID("user");
            //按用户ID查询用户角色列表
            IList<User_RoleConfig> iUrList = iUrc.GetList("UserID=" + userID, null, "", 0);
            //遍历角色列表
            foreach (User_RoleConfig ur in iUrList)
            {
                //按角色ID查询模块列表
                IList<User_RolePermissionConfig> iUrpcList = iUrpc.GetList("RoleID=" + ur.RoleID, null, "", 0);

                //遍历模块列表
                foreach (User_RolePermissionConfig uprc in iUrpcList)
                {
                    //如果新模块列表不存在当前ID
                    if (!IsExists(menuList, uprc.MenuID))
                    {
                        //向新模块列表插入当前ID
                        menuList.Add(uprc.MenuID);
                    }
                }
            }
            return menuList;
        }

        /// <summary>
        /// 判断ID是否存在与ArrayList中
        /// </summary>
        /// <param name="ArrayList">列表</param>
        /// <param name="ID">ID</param>
        /// <returns></returns>
        private bool IsExists(ArrayList list, int ID)
        {
            if (list == null) { return false; }
            foreach (int id in list)
            {
                if (id.Equals(ID))
                    return true;
            }
            return false;
        }

        /// <summary>
        /// 判断当前菜单是否是叶节点
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        private string IsLeaf(int ID)
        {
            IList<User_TreeMenu> list = iUtm.GetList("ParentID=" + ID, null, "Sort", 0);
            if (list.Count > 0)
            {
                foreach (User_TreeMenu utm in list)
                {
                    if (IsExists(menus, utm.ID))
                    {
                        return "false";
                    }
                }
            }
            return "true";
        }

        /// <summary>
        /// 递归树
        /// </summary>
        /// <param name="parentID"></param>
        private void TreeData(int parentID)
        {
            MsSqlDal<User_TreeMenu> iUtm = new MsSqlDal<User_TreeMenu>();
            IList<User_TreeMenu> list = iUtm.GetList("ParentID=" + parentID, null, "Sort", 0);
            strTree += "[";
            foreach (User_TreeMenu utm in list)
            {
                if (IsExists(menus, utm.ID))
                {
                    strTree += "{\"id\": \"myTree." + utm.ID + "." + utm.Text + "\",\"iconCls\": \"" + utm.IconCls + "\",\"isClass\":" + (utm.IsClass ? "true" : "false") + ",\"text\":\"" + utm.Text + "\",\"singleClickExpand\":" + (utm.SingleClickExpand ? "true" : "false") + ",\"cls\":\"" + utm.Cls + "\",\"href\":\"" + utm.Href + "\",\"leaf\":" + IsLeaf(utm.ID);
                    if (!IsLeaf(utm.ID).Equals("true"))
                    {
                        strTree += ",\"children\":";
                        TreeData(utm.ID);
                    }
                    strTree += "},";
                }
            }
            strTree = strTree.TrimStart(',');
            strTree = strTree.TrimEnd(',');
            strTree += "]";
        }
    }
}